package com.hb.trade.api.routine.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import com.hb.core.pojo.center.sys.entity.DicCode;
import com.hb.core.pojo.project.section.dto.BidSectionDTO;
import com.hb.trade.client.center.DictionaryClient;
import lombok.extern.slf4j.Slf4j;
import org.fzzn.core.model.msg.Message;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 标段数据逻辑处理service
 *
 * @author zhaojk
 */
@Slf4j
@Service
public class BidSectionService {

    protected final DictionaryClient dictionaryClient;

    public BidSectionService(DictionaryClient dictionaryClient) {
        this.dictionaryClient = dictionaryClient;
    }

    /**
     * 查询标段列表中标段状态的显示值
     *
     * @param sections 必须为标段信息列表
     */
    public List<BidSectionDTO> displaySectionStatuses(List<?> sections) {
        if (CollUtil.isEmpty(sections)) {
            return CollUtil.list(false);
        }

        List<BidSectionDTO> newSections = CollUtil.list(false);
        for (Object sectionObj : sections) {
            BidSectionDTO section = BeanUtil.copyProperties(sectionObj, BidSectionDTO.class);
            newSections.add(section);

            Long sectionStatus = section.getSectionStatus();
            // 如果不是标段对象, 忽略
            if (sectionStatus == null) {
                continue;
            }
            // 该标段的当前标段状态
            Message<DicCode> dicCode = dictionaryClient.getByPrimary(sectionStatus);
            if (dicCode.isSuccess()) {
                DicCode data = dicCode.getData();
                section.setSimpleName(data.getSimpleName());
            }
        }
        return newSections;
    }
}
